﻿using JsonConversion;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Web.Mvc;
using RTracker.Models.Common;
using RTracker.Models.ErrorLog;
using System.Web.Security;

namespace RTracker.Areas.Recruitment.Models.Vacancy
{
    public class MVacancy
    {
        /// static variable RowSize from config settings to get list of this row size.
        public static int nRowSize = Convert.ToInt32(ConfigurationManager.AppSettings["TotalRecordsInListVacancy"]);

        /// <summary>
        /// This method is used to create a Vacancy in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details</returns>
        public CStatus CreateVacancy(CAuthenticationContext objCAuthenticationContext, ref CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();
          
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_create_vacancy_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancy.OrganizationId;
                    cmd.Parameters.Add("@client_id", SqlDbType.Int).Value = objCVacancy.ClientId;
                    cmd.Parameters.Add("@opening_date", SqlDbType.Date).Value = objCVacancy.OpeningDate;
                    cmd.Parameters.Add("@department", SqlDbType.Int).Value = objCVacancy.Department;
                    cmd.Parameters.Add("@vacancy_name", SqlDbType.NVarChar).Value = objCVacancy.VacancyName;
                    cmd.Parameters.Add("@vacancy_desc", SqlDbType.NVarChar).Value = objCVacancy.VacancyDesc;
                    cmd.Parameters.Add("@shift", SqlDbType.Int).Value = objCVacancy.Shift;
                    cmd.Parameters.Add("@status", SqlDbType.Int).Value = objCVacancy.Status;
                    cmd.Parameters.Add("@allocated_to", SqlDbType.Int).Value = objCVacancy.AllocatedTo;
                    cmd.Parameters.Add("@technical_skills", SqlDbType.NVarChar).Value = objCVacancy.TechnicalSkills;
                    cmd.Parameters.Add("@keywords", SqlDbType.NVarChar).Value = objCVacancy.Keywords;
                    cmd.Parameters.Add("@expirence_range", SqlDbType.Int).Value = objCVacancy.ExpirenceRange;
                    cmd.Parameters.Add("@salary_range", SqlDbType.Int).Value = objCVacancy.SalaryRange;
                    cmd.Parameters.Add("@notice_period_days", SqlDbType.Int).Value = objCVacancy.NoticePeriodDays;
                    cmd.Parameters.Add("@roles_responsibilities", SqlDbType.NVarChar).Value = objCVacancy.RolesResponsibilities;
                     cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// This method is used to delete a Vacancy in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about  authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus DeleteVacancy(CAuthenticationContext objCAuthenticationContext, ref CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_delete_vacancy_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    cmd.Parameters.Add("@Vacancy_ids", SqlDbType.NVarChar).Value = objCVacancy.VacancyIds;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// This method is used to update the Vacancy's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus UpdateVacancy(CAuthenticationContext objCAuthenticationContext, ref CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_update_vacancy_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_id", SqlDbType.Int).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancy.OrganizationId;
                    cmd.Parameters.Add("@client_id", SqlDbType.Int).Value = objCVacancy.ClientId;
                    cmd.Parameters.Add("@opening_date", SqlDbType.Date).Value = objCVacancy.OpeningDate;
                    cmd.Parameters.Add("@department", SqlDbType.Int).Value = objCVacancy.Department;
                    cmd.Parameters.Add("@vacancy_name", SqlDbType.NVarChar).Value = objCVacancy.VacancyName;
                    cmd.Parameters.Add("@vacancy_desc", SqlDbType.NVarChar).Value = objCVacancy.VacancyDesc;
                    cmd.Parameters.Add("@shift", SqlDbType.Int).Value = objCVacancy.Shift;
                    cmd.Parameters.Add("@status", SqlDbType.Int).Value = objCVacancy.Status;
                    cmd.Parameters.Add("@allocated_to", SqlDbType.Int).Value = objCVacancy.AllocatedTo;
                    cmd.Parameters.Add("@technical_skills", SqlDbType.NVarChar).Value = objCVacancy.TechnicalSkills;
                    cmd.Parameters.Add("@keywords", SqlDbType.NVarChar).Value = objCVacancy.Keywords;
                    cmd.Parameters.Add("@expirence_range", SqlDbType.Int).Value = objCVacancy.ExpirenceRange;
                    cmd.Parameters.Add("@salary_range", SqlDbType.Int).Value = objCVacancy.SalaryRange;
                    cmd.Parameters.Add("@notice_period_days", SqlDbType.Int).Value = objCVacancy.NoticePeriodDays;
                    cmd.Parameters.Add("@roles_responsibilities", SqlDbType.NVarChar).Value = objCVacancy.RolesResponsibilities;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;
                    

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }

            return oCStatus;
        }

        /// <summary>
        /// This method is used to get the Vacancy's detail in the database.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about  authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus GetVacancyDetail(CAuthenticationContext objCAuthenticationContext, ref CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_vacancy_detail_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;

                    cmd.Parameters.Add("@vacancy_id", SqlDbType.Int).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;

                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    if (rowsAffected > 0)
                    {
                        objCVacancy = dt.ConvertToListFromDataTableVacancy()[0];
                    }

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.DetailSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }
            return oCStatus;
        }

        /// <summary>
        /// Sets the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about Vacancy's authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetVacancySoftLock(CAuthenticationContext objCAuthenticationContext, CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_vacancy_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_id", SqlDbType.NVarChar).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Sets the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about Vacancy's authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus SetVacancyHardLock(CAuthenticationContext objCAuthenticationContext, CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_set_vacancy_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@vacancy_id", SqlDbType.NVarChar).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the softlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about Vacancy's authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseVacancySoftLock(CAuthenticationContext objCAuthenticationContext, CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_Vacancy_soft_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Vacancy_id", SqlDbType.NVarChar).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        /// Releases the hardlock on the record whose Id is passed.
        /// </summary>
        /// <param name="objCAuthenticationContext">This object contains information about Vacancy's authentication context.</param>
        /// <param name="objCVacancy">This object contains details of the Vacancy.</param>
        /// <returns>CStatus which is an object that contain transaction details.</returns>
        public CStatus ReleaseVacancyHardLock(CAuthenticationContext objCAuthenticationContext, CVacancy objCVacancy)
        {
            CStatus oCStatus = new CStatus();

            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    con.Open();
                    SqlCommand cmd = new SqlCommand("r_release_Vacancy_hard_lock_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Vacancy_id", SqlDbType.NVarChar).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@created_by", SqlDbType.Int).Value = objCAuthenticationContext.UserId;

                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;

                    cmd.ExecuteNonQuery();

                    if (Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()) > 0)
                    {
                        oCStatus.Success = true;
                        oCStatus.StatusDetails = CAppConstants.SessionSuccess;
                    }
                    else
                    {
                        oCStatus.Success = false;
                        oCStatus.StatusDetails = Convert.ToInt32(cmd.Parameters["@idOut"].Value.ToString()).DbError();
                        //oCStatus.StatusDetails = CAppErrorsConstants.SomethingWentWrong;
                    }
                }
                catch (Exception ex)
                {
                    oCStatus.Success = false;
                    oCStatus.StatusDetails = ex.Message;
                }
            }


            return oCStatus;
        }

        /// <summary>
        ///  This function is used to get complete Vacancy list
        /// </summary>
        /// <returns>List of type CVacancy</returns>
        public GVacancy GetVacancyGridList(CVacancy objCVacancy, CAuthenticationContext objCAuthenticationContext)
        {
            GVacancy objGVacancy = new GVacancy();
            int nTotalRecords = 0;
            int nCurrentRowSize = nRowSize;

            if (objCVacancy.RowSize != 0)
            {
                nCurrentRowSize = objCVacancy.RowSize;
            }
            using (SqlConnection con = new SqlConnection(CCommonCollection.ConnectionString))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand("r_get_Vacancy_paging_list_procedure", con);
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@Vacancy_id", SqlDbType.Int).Value = objCVacancy.VacancyId;
                    cmd.Parameters.Add("@organization_id", SqlDbType.Int).Value = objCVacancy.OrganizationId;
                    cmd.Parameters.Add("@page_number", SqlDbType.Int).Value = objCVacancy.PageNumber;
                    cmd.Parameters.Add("@row_size", SqlDbType.Int).Value = nCurrentRowSize;
                    cmd.Parameters.Add("@sort_on", SqlDbType.VarChar).Value = objCVacancy.SortOn;
                    cmd.Parameters.Add("@sort_type", SqlDbType.VarChar).Value = objCVacancy.SortType;
                    cmd.Parameters.Add("@search", SqlDbType.VarChar).Value = objCVacancy.Search;
                    cmd.Parameters.Add("@searchon", SqlDbType.VarChar).Value = objCVacancy.SearchOn;
                    cmd.Parameters.Add("@idOut", SqlDbType.Int).Value = null;
                    cmd.Parameters["@idOut"].Direction = ParameterDirection.Output;
                    cmd.Parameters.Add("@error", SqlDbType.Int).Value = null;
                    cmd.Parameters["@error"].Direction = ParameterDirection.Output;
                    SqlDataAdapter adp = new SqlDataAdapter(cmd);

                    DataTable dt = new DataTable();

                    int rowsAffected = adp.Fill(dt);

                    objGVacancy.Paging = new CPaging();
                    if (rowsAffected > 0)
                    {
                        objGVacancy.LstVacancy = dt.ConvertToListFromDataTableVacancy();
                        objGVacancy.LstVacancy[0].PageNumber = int.Parse(cmd.Parameters["@idOut"].Value.ToString());
                        objGVacancy.Paging.PageNumber = objGVacancy.LstVacancy[0].PageNumber;
                    }
                    else
                    {
                        objGVacancy.LstVacancy = null;
                    }


                    nTotalRecords = int.Parse(cmd.Parameters["@error"].Value.ToString());

                    objGVacancy.Status = new CStatus();

                    if (nTotalRecords < 0)
                    {
                        objGVacancy.Status.Success = false;
                        objGVacancy.Status.StatusDetails = nTotalRecords.DbError();
                    }
                    else
                    {
                        objGVacancy.Paging.TotalRecordsCount = nTotalRecords;
                        objGVacancy.Paging.PageSize = nCurrentRowSize;

                        objGVacancy.Status.Success = true;
                        objGVacancy.Status.StatusDetails = "success";
                    }
                }
                catch (Exception ex)
                {
                    objGVacancy.Status = new CStatus();
                    objGVacancy.Status.Success = false;
                    objGVacancy.Status.StatusDetails = ex.Message;
                }
            }
            return objGVacancy;
        }
    }
}